package com.bdcom.autotest.interaction.http;

public class IpCaculate {

	/**
	 * 根据掩码位数计算掩码
	 * 
	 * @param maskIndex 掩码位
	 * @return 子网掩码
	 */
	public static String getNetMask(String maskIndex) {
		StringBuilder mask = new StringBuilder();
		Integer inetMask = 0;
		try {
			inetMask = Integer.parseInt(maskIndex);
		} catch (NumberFormatException e) {
			System.out.println(e.getMessage());
			return null;
		}
		if (inetMask > 32) {
			return null;
		}
		// 子网掩码为1占了几个字节
		int num1 = inetMask / 8;
		// 子网掩码的补位位数
		int num2 = inetMask % 8;
		int array[] = new int[4];
		for (int i = 0; i < num1; i++) {
			array[i] = 255;
		}
		for (int i = num1; i < 4; i++) {
			array[i] = 0;
		}
		for (int i = 0; i < num2; num2--) {
			array[num1] += 1 << 8 - num2;
		}
		for (int i = 0; i < 4; i++) {
			if (i == 3) {
				mask.append(array[i]);
			} else {
				mask.append(array[i] + ".");
			}
		}
		return mask.toString();
	}

	/**
	 * 根据网段计算起始IP 网段格式:x.x.x.x/x 一个网段0一般为网络地址,255一般为广播地址. 起始IP计算:网段与掩码相与之后加一的IP地址
	 * 
	 * @param segment 网段
	 * @return 起始IP
	 */
	public static String getStartIp(String segment) {
		StringBuffer startIp = new StringBuffer();
		if (segment == null) {
			return null;
		}
		String arr[] = segment.split("/");
		String ip = arr[0];
		String maskIndex = arr[1];
		String mask = IpCaculate.getNetMask(maskIndex);
		if (4 != ip.split("\\.").length || mask == null) {
			return null;
		}
		int ipArray[] = new int[4];
		int netMaskArray[] = new int[4];
		for (int i = 0; i < 4; i++) {
			try {
				ipArray[i] = Integer.parseInt(ip.split("\\.")[i]);
				netMaskArray[i] = Integer.parseInt(mask.split("\\.")[i]);
				if (ipArray[i] > 255 || ipArray[i] < 0 || netMaskArray[i] > 255 || netMaskArray[i] < 0) {
					return null;
				}
				ipArray[i] = ipArray[i] & netMaskArray[i];
				if (i == 3) {
					startIp.append(ipArray[i] + 1);
				} else {
					startIp.append(ipArray[i] + ".");
				}
			} catch (NumberFormatException e) {
				System.out.println(e.getMessage());
			}
		}
		return startIp.toString();
	}

	/**
	 * 根据网段计算结束IP
	 * 
	 * @param segment
	 * @return 结束IP
	 */
	public static String getEndIp(String segment) {
		StringBuffer endIp = new StringBuffer();
		String startIp = getStartIp(segment);
		if (segment == null) {
			return null;
		}
		String arr[] = segment.split("/");
		String maskIndex = arr[1];
		// 实际需要的IP个数
		int hostNumber = 0;
		int startIpArray[] = new int[4];
		try {
			hostNumber = 1 << 32 - (Integer.parseInt(maskIndex));
			for (int i = 0; i < 4; i++) {
				startIpArray[i] = Integer.parseInt(startIp.split("\\.")[i]);
				if (i == 3) {
					startIpArray[i] = startIpArray[i] - 1;
					break;
				}
			}
			startIpArray[3] = startIpArray[3] + (hostNumber - 1);
		} catch (NumberFormatException e) {
			System.out.println(e.getMessage());
		}

		if (startIpArray[3] > 255) {
			int k = startIpArray[3] / 256;
			startIpArray[3] = startIpArray[3] % 256;
			startIpArray[2] = startIpArray[2] + k;
		}
		if (startIpArray[2] > 255) {
			int j = startIpArray[2] / 256;
			startIpArray[2] = startIpArray[2] % 256;
			startIpArray[1] = startIpArray[1] + j;
			if (startIpArray[1] > 255) {
				int k = startIpArray[1] / 256;
				startIpArray[1] = startIpArray[1] % 256;
				startIpArray[0] = startIpArray[0] + k;
			}
		}
		for (int i = 0; i < 4; i++) {
			if (i == 3) {
				startIpArray[i] = startIpArray[i] - 1;
			}
			if ("" == endIp.toString() || endIp.length() == 0) {
				endIp.append(startIpArray[i]);
			} else {
				endIp.append("." + startIpArray[i]);
			}
		}
		return endIp.toString();
	}
	
	
	/**
	 * 把xx.xx.xx.xx类型的转为long类型的
	 *
	 * @param ip
	 * @return
	 */
	public static Long getIpFromString(String ip) {
		// 进制 2^8 = 256 共四段
		Long ipLong = 0L;
		String ipTemp = ip;
		ipLong = ipLong * 256 + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.')));
		ipTemp = ipTemp.substring(ipTemp.indexOf('.') + 1, ipTemp.length());
		ipLong = ipLong * 256 + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.')));
		ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length());
		ipLong = ipLong * 256 + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.')));
		ipTemp = ipTemp.substring(ipTemp.indexOf('.') + 1, ipTemp.length());
		ipLong = ipLong * 256 + Long.parseLong(ipTemp);
		return ipLong;
	}

	/**
	 * 把long类型的Ip转为一般Ip类型：xx.xx.xx.xx
	 *
	 * @param ip
	 * @return
	 */
	public static String getIpFromLong(Long ip) {
		String s1 = String.valueOf((ip & 4278190080L) / 16777216L); // 255.0.0.0 ~ 1.0.0.0 截取第一个八位
		String s2 = String.valueOf((ip & 16711680L) / 65536L); // 0.255.0.0 ~ 0.1.0.0 截取第二个八位
		String s3 = String.valueOf((ip & 65280L) / 256L); // 0.0.255.0 ~ 0.0.1.0 截取第三个八位
		String s4 = String.valueOf(ip & 255L); // 0.0.0.255 ~ 0.0.0.1 截取第四个八位
		return s1 + "." + s2 + "." + s3 + "." + s4;
	}

	
}